<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="68px">
			<el-form-item label="月份" prop="status">
				<el-date-picker v-model="queryParams.month" type="month" format="yyyy-MM" value-format="yyyy-MM" placeholder="选择月份" @change="handleQuery"></el-date-picker>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>
		
		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
					v-hasPermi="['bpmforns:reportforms:export']">导出</el-button>
				<el-button type="primary" plain size="mini" v-hasPermi="['bpmforns:reportforms:upMileage']" @click="submitUpMileage">保存</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table border v-loading="loading" :data="reportformsList" @selection-change="handleSelectionChange">
			<el-table-column label="序号" align="center" prop="id" width="50px" />
			<el-table-column label="牌照号码" align="center" prop="licensePlate" />
			<el-table-column label="车型" align="center" prop="carName" />
			<el-table-column label="起始里程" align="center" prop="startMileage">
				<template slot-scope="scope">
					<el-input placeholder="请输入起始里程" v-model="scope.row.startMileage" clearable v-if="checkPermi(['bpmforns:reportforms:upMileage'])"></el-input>
					<div v-else>{{ scope.row.startMileage }}</div>
				</template>
			</el-table-column>
			<el-table-column label="终止里程" align="center" prop="endMileage">
				<template slot-scope="scope">
					<el-input placeholder="请输入终止里程" v-model="scope.row.endMileage" clearable v-if="checkPermi(['bpmforns:reportforms:upMileage'])"></el-input>
					<div v-else>{{ scope.row.endMileage }}</div>
				</template>
			</el-table-column>
			<el-table-column label="车辆行驶路线" align="center">
				<el-table-column label="起始" align="center" prop="startLocation" />
				<el-table-column label="中转" align="center" prop="transitPlace">
					<template slot-scope="scope">
						{{ scope.row.transitPlace ? scope.row.transitPlace.join(' - ') : '-' }}
					</template>
				</el-table-column>
				<el-table-column label="目的地" align="center" prop="endLocation" />
			</el-table-column>
			<el-table-column label="申请时间" align="center" prop="createTime" width="180" />
			<el-table-column label="用车时间" align="center" prop="startTime" width="180" />
			<el-table-column label="司机" align="center" prop="driverName" />
			<el-table-column label="车况" align="center" prop="status">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.car_status" :value="scope.row.status" />
				</template>
			</el-table-column>
			<el-table-column label="用车人" align="center" prop="nickNames" />
			<el-table-column label="备注" align="center" prop="remark" />
		</el-table>
	</div>
</template>

<script>
	import {
		listReportforms,
		getReportforms,
		delReportforms,
		addReportforms,
		updateReportforms,
		upMileage
	} from "@/api/bpmforns/reportforms";
	import { checkPermi } from "@/utils/permission"; // 权限判断函数
	export default {
		name: "Reportforms",
		dicts: ['car_status'],
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 用车申请报表表格数据
				reportformsList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					month: '',
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {}
			};
		},
		created() {
			let nowDate = new Date();
			let date = {
				year: nowDate.getFullYear(),
				month: nowDate.getMonth() + 1,
				day: nowDate.getDate()
			};
			
			this.queryParams.month = date.year + "-" + (date.month >= 10 ? date.month : "0" + date.month);
			this.getList();
		},
		methods: {
			checkPermi,
			/** 查询用车申请报表列表 */
			getList() {
				this.loading = true;
				listReportforms(this.queryParams).then(response => {
					this.reportformsList = response.data;
					console.log('列表内容' , this.reportformsList);
					// this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					id: null,
					licensePlate: null,
					carName: null,
					startLocation: null,
					transitPlace: null,
					endLocation: null,
					startTime: null,
					endTime: null,
					status: null,
					remark: null,
					fillTime: null,
					delFlag: null,
					createBy: null,
					createTime: null,
					updateBy: null,
					updateTime: null
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				console.log('查询内容' , this.queryParams);
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "添加用车申请报表";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const id = row.id || this.ids
				getReportforms(id).then(response => {
					this.form = response.data;
					this.open = true;
					this.title = "修改用车申请报表";
				});
			},
			/**
			 * 更新里程
			 */
			submitUpMileage(){
				upMileage(this.reportformsList).then(res => {
					console.log('返回结构' , res);
					this.$modal.msgSuccess("更新成功");
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.id != null) {
							updateReportforms(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addReportforms(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const ids = row.id || this.ids;
				this.$modal.confirm('是否确认删除用车申请报表编号为"' + ids + '"的数据项？').then(function() {
					return delReportforms(ids);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('bpmforns/reportforms/export', {
					...this.queryParams
				}, `总裁办车辆管理月报表_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>